﻿using Engine;
using Engine.Graphics;
using Game;

namespace IndustrialAge
{
	public class SubsystemSight : SubsystemBlockBehavior, IDrawable
	{
		public PrimitivesRenderer3D m_primitivesRenderer3d = new PrimitivesRenderer3D();

		public Vector3?[] m_lightningStrikePosition = new Vector3?[9999];

		public Vector3?[] m_lightningStartPosition = new Vector3?[9999];

		public float?[] m_lightningStrikeBrightness = new float?[9999];

		public float?[] m_lightningStrikeBrightness2 = new float?[9999];

		public float?[] m_lightningStrikeBrightness3 = new float?[9999];

		public FlatBatch3D flatBatch3D;

		//public FlatBatch2D flatBatch2D;

		public int[] m_drawOrders = new[] { 500 };

		public int[] DrawOrders => m_drawOrders;

		public override int[] HandledBlocks => new[] { ChemicalBlock.Index };

		public void MakeLightningStrike(Vector3 targetPosition, int type)
		{
			for (int nu1 = 0; nu1 < m_lightningStrikePosition.Length; nu1++)
			{
				if (m_lightningStrikePosition[nu1] == null)
				{
					m_lightningStrikePosition[nu1] = targetPosition;
					//m_lightningStartPosition[nu1] = startPosition;
					m_lightningStrikeBrightness[nu1] = 25f;
					m_lightningStrikeBrightness2[nu1] = type;
					//m_lightningStrikeBrightness3[nu1] = 1f;
					break;
				}
			}
		}

		public void ClearSight()
		{
			for (int nu1 = 0; nu1 < m_lightningStrikePosition.Length; nu1++)
			{
				
					m_lightningStrikePosition[nu1] = null;
					m_lightningStartPosition[nu1] = null;
					m_lightningStrikeBrightness[nu1] = null;
					m_lightningStrikeBrightness2[nu1] = null;
					m_lightningStrikeBrightness3[nu1] = null;
				
			}
		}

		public void Draw(Camera camera, int drawOrder)
		{
			flatBatch3D = m_primitivesRenderer3d.FlatBatch(0, DepthStencilState.DepthRead, null, BlendState.Additive);
			//if (Utils.SubsystemTime.PeriodicGameTimeEvent(0.05, 0.0))
			for (int nu1 = 0; nu1 < m_lightningStrikePosition.Length; nu1++)
			{
				if (m_lightningStrikeBrightness[nu1].HasValue)
				{
					Color coll = Color.LightBlue;
					if (m_lightningStrikeBrightness2[nu1] == 2f)
						coll = Color.LightRed;
					float size = 1f;// m_lightningStrikeBrightness3[nu1].Value;
					Vector3 p = m_lightningStrikePosition[nu1].Value;
					float p2 = 0.05f;
					BoundingBox Box = new BoundingBox(p.X,p.Y,p.Z, p.X+1f, p.Y+1f, p.Z+1f);
					BoundingBox Box2 = new BoundingBox(p.X-p2, p.Y-p2, p.Z-p2, p.X + 1f+p2, p.Y + 1f+p2, p.Z + 1f+p2);
					//Vector3 p2 = m_lightningStartPosition[nu1].Value;
					flatBatch3D.QueueBoundingBox(Box, coll);
					flatBatch3D.QueueBoundingBox(Box2, coll);
					//flatBatch2D.
					//flatBatch3D.QueueQuad(p + new Vector3(0, 0.01f*size, 0), p2 + new Vector3(0, 0.01f*size, 0), p - new Vector3(0, 0.01f * size, 0), p2 - new Vector3(0, 0.01f * size, 0), coll);
					//flatBatch3D.QueueQuad(p2 + new Vector3(0, 0.01f * size, 0), p + new Vector3(0, 0.01f*size, 0), p2 - new Vector3(0, 0.01f * size, 0), p - new Vector3(0, 0.01f * size, 0), coll);
					//flatBatch3D.QueueQuad(p + new Vector3(0, 0, 0.01f * size), p2 + new Vector3(0, 0, 0.01f * size), p - new Vector3(0, 0, 0.01f * size), p2 - new Vector3(0, 0, 0.01f * size), coll);
					//flatBatch3D.QueueQuad(p2 + new Vector3(0, 0, 0.01f * size), p + new Vector3(0, 0, 0.01f * size), p2 - new Vector3(0, 0, 0.01f * size), p - new Vector3(0, 0, 0.01f * size), coll);
					//flatBatch3D.QueueQuad(p + new Vector3(0.01f * size, 0, 0), p2 + new Vector3(0.01f * size, 0, 0), p - new Vector3(0.01f * size, 0, 0), p2 - new Vector3(0.01f * size, 0, 0), coll);
					//flatBatch3D.QueueQuad(p2 + new Vector3(0.01f * size, 0, 0), p + new Vector3(0.01f * size, 0, 0), p2 - new Vector3(0.01f * size, 0, 0), p - new Vector3(0.01f * size, 0, 0), coll);
					m_lightningStrikeBrightness[nu1] -= Utils.SubsystemTime.GameTimeDelta / 0.8f * 3;
					if (m_lightningStrikeBrightness[nu1] <= 0f)
					{
						m_lightningStrikePosition[nu1] = null;
						m_lightningStartPosition[nu1] = null;
						m_lightningStrikeBrightness[nu1] = null;
						m_lightningStrikeBrightness2[nu1] = null;
					}
				}
			}
			m_primitivesRenderer3d.Flush(camera.ViewProjectionMatrix);
		}

		public override bool OnInteract(TerrainRaycastResult raycastResult, ComponentMiner componentMiner)
		{
			CellFace cellFace = raycastResult.CellFace;
			if (ChemicalBlock.Get(Utils.Terrain.GetCellValueFast(cellFace.X, cellFace.Y, cellFace.Z)) is Cylinder)
			{
				SubsystemTerrain.ChangeCell(cellFace.X, cellFace.Y, cellFace.Z, ItemBlock.IdTable["钢瓶"]);
			}
			return false;
		}

		public override bool OnAim(Ray3 aim, ComponentMiner componentMiner, AimState state)
		{
			Vector3 start = aim.Position, direction=aim.Direction;
			if (ChemicalBlock.Get(componentMiner.ActiveBlockValue) is Bottle)
			{
				if (state == AimState.Completed)
				{
					var inventory = componentMiner.Inventory;
					int activeSlotIndex = inventory.ActiveSlotIndex, count = inventory.GetSlotCount(activeSlotIndex);
					inventory.RemoveSlotItems(activeSlotIndex, count);
					inventory.AddSlotItems(activeSlotIndex, ItemBlock.IdTable["Bottle"], count);
				}
				return state != AimState.InProgress;
			}
			return false;
		}
	}
}